package matt.design;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This class controls various password functions and what happens
 * when various buttons on the page are clicked
 * 
 * Version 1.0 - Each button return a user to the login page
 * 
 * @author Crunch Time
 * @version 1.0
 */
public class PasswordFunctionActivity extends Activity {
    @Override
    /**
     * This method determines what happens when a button is 
	 * clicked on the password page
     * 
     * @param savedInstanceState
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setting default screen to login.xml
        setContentView(R.layout.passwordfunctions);
 
        Button getPass = (Button) findViewById(R.id.btnsubmitemail);
        Button newPass = (Button) findViewById(R.id.btnsumbitnewpassword);
        Button retnLog = (Button) findViewById(R.id.btn_back_to_login);
        
        // Listening to register new account link
        getPass.setOnClickListener(new View.OnClickListener() { 			
    			/**
    			 * Returns the user to the login screen
    			 * 
    			 * @param v
    			 */
    			public void onClick(View v) {
                    // Closing registration screen
    				// Switching to Login Screen/closing register screen
    				finish();
    			}
    		});
        
        newPass.setOnClickListener(new View.OnClickListener() { 			
        	/**
			 * Returns the user to the login screen
			 * 
			 * @param v
			 */
			public void onClick(View v) {
                // Closing registration screen
				// Switching to Login Screen/closing register screen
				EditText email = (EditText) findViewById(R.id.ps_email);
				EditText oldPass = (EditText) findViewById(R.id.ps_oldPassword);
				EditText newPass = (EditText) findViewById(R.id.ps_newPassword);
				EditText checkNew = (EditText) findViewById(R.id.ps_newPasswordCheck);
				TextView error = (TextView) findViewById(R.id.ps_errortxt);
				
				Context c = getApplicationContext();
				UsersDataSource datasource = new UsersDataSource(c);
				datasource.open();
				
				Boolean changed = datasource.changePassword(email.getText().toString(), oldPass.getText().toString(), newPass.getText().toString(), checkNew.getText().toString());
				
				if(changed){
					finish();
				}
				else{
					error.setText("Wrong username/password or new password does not match");
				}
					
			}
		});
        
        retnLog.setOnClickListener(new View.OnClickListener() { 			
			/**
			 * Returns the user to the login screen
			 * 
			 * @param v
			 */
			public void onClick(View v) {
                // Closing registration screen
				// Switching to Login Screen/closing register screen
				finish();
			}
		});
    }
}